package com.example.g32.login

import android.os.Bundle
import android.util.Log
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.base.entity.LoginEntity
import com.example.base.http.Cons
import com.example.base.mvi.BaseActivity
import com.example.base.mvi.UiState
import com.example.g32.R
import com.example.g32.databinding.ActivityLoginBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
@AndroidEntryPoint
@Route(path = "/app/LoginActivity")
class LoginActivity : BaseActivity<ActivityLoginBinding, LoginIntent, LoginViewModel>() {
    override val binding by lazy {
        ActivityLoginBinding.inflate(layoutInflater)
    }
    override val vm by viewModels<LoginViewModel>()

    override fun initView() {
        if (SPUtils.getInstance().getString(Cons.TOKEN, "").isNotEmpty()){
            ARouter.getInstance().build("/app/MainActivity").navigation()
        }

        binding.login.setOnClickListener {
            if (binding.password.text.isNullOrEmpty() && binding.username.text.isNullOrEmpty()){
                ToastUtils.showLong("账号密码不能为空")
            }else{
                val username = binding.username.text.toString()
                val password = binding.password.text.toString()
                vm.sendIntent(LoginIntent.login(username, password))
               // vm.sendIntent(LoginIntent.loginRoom(username, password))
            }
        }

        binding.goReg.setOnClickListener {
            ARouter.getInstance().build("/app/ReqActivity").navigation()
        }
    }

    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*>->{
                        val bean = it.data as LoginEntity
                        ARouter.getInstance().build("/app/SpiashActivity").navigation()
                        SPUtils.getInstance().apply {
                            put(Cons.TOKEN, bean.token)
                            put(Cons.ROLE, bean.roleid)
                            put(Cons.UID, bean.id)
                            put(Cons.UNAME, bean.username)
                        }
                        //根据登录的用户角色跳转不同页面(如：管理员、普通用户)
                        start()
                    }
                    is UiState.onError->{
                        Log.d("xs", it.msg)
                    }
                    else->{}
                }
            }
        }
    }
    fun start() {
        // 19为管理员 20为普通用户
        val role = SPUtils.getInstance().getInt(Cons.ROLE, -1)
        if (role == 19) {
            ARouter.getInstance().build("/app/MainActivity").navigation()
            finish()
        } else if (role == 20) {
            ARouter.getInstance().build("/app/ReqActivity").navigation()
            finish()
        }
    }

}